remove(list = ls())
library("foreign")
library("car")
library("haven")
library("psych")
library("xtable")

location <- "/Users/dillonlaaker/Box Sync/Stability/"
setwd(paste(location, "data/datasets", sep = ""))

ines <- read.dta("ines_data.dta")
bes <- read.dta("bes_data.dta")
taps <- read.dta("taps_data.dta")
ncp <- read.dta("ncp_data.dta")
shp <- read.dta("shp_data.dta")
liss <- read.dta("liss_data.dta")
gles <- read.dta("gles_data.dta")
cces <- read.dta("cces_data.dta")
vsg <- read.dta("vsg_data.dta")

##Correlations
lisscor <- cor(liss$immigration_8, liss$immigration_17, method="spearman", use="pairwise")
lisscor <- format(round(lisscor, digits=3), nsmall = 3)
lisscorall <- cor(liss$immigration_8[liss$all==1], liss$immigration_17[liss$all==1], method="spearman", use="pairwise")
lisscorall <- format(round(lisscorall, digits=3), nsmall = 3)

liss1cor <- cor(liss$imm1_8, liss$imm1_17, method="spearman", use="pairwise")
liss2cor <- cor(liss$imm2_8, liss$imm2_17, method="spearman", use="pairwise")
liss3cor <- cor(liss$imm3_8, liss$imm3_17, method="spearman", use="pairwise")
liss4cor <- cor(liss$imm4_8, liss$imm4_17, method="spearman", use="pairwise")
liss5cor <- cor(liss$imm5_8, liss$imm5_17, method="spearman", use="pairwise")
liss6cor <- cor(liss$imm6_8, liss$imm6_17, method="spearman", use="pairwise")
lisscoravg <- (liss1cor + liss2cor + liss3cor + liss4cor + liss5cor + liss6cor)/6
lisscoravg <- format(round(lisscoravg, digits=3), nsmall = 3)

liss1corall <- cor(liss$imm1_8[liss$all==1], liss$imm1_17[liss$all==1], method="spearman", use="pairwise")
liss2corall <- cor(liss$imm2_8[liss$all==1], liss$imm2_17[liss$all==1], method="spearman", use="pairwise")
liss3corall <- cor(liss$imm3_8[liss$all==1], liss$imm3_17[liss$all==1], method="spearman", use="pairwise")
liss4corall <- cor(liss$imm4_8[liss$all==1], liss$imm4_17[liss$all==1], method="spearman", use="pairwise")
liss5corall <- cor(liss$imm5_8[liss$all==1], liss$imm5_17[liss$all==1], method="spearman", use="pairwise")
liss6corall <- cor(liss$imm6_8[liss$all==1], liss$imm6_17[liss$all==1], method="spearman", use="pairwise")
lisscoravgall <- (liss1corall + liss2corall + liss3corall + liss4corall + liss5corall + liss6corall)/6
lisscoravgall <- format(round(lisscoravgall, digits=3), nsmall = 3)

bescor <- cor(bes$imm1, bes$imm11, method="spearman", use="pairwise")
bescor <- format(round(bescor, digits=3), nsmall = 3)
bescorall <- cor(bes$imm1[bes$all==1], bes$imm11[bes$all==1], method="spearman", use="pairwise")
bescorall <- format(round(bescorall, digits=3), nsmall = 3)

bescorecon <- cor(bes$immecon1, bes$immecon11, method="spearman", use="pairwise")
bescorcul <- cor(bes$immcul1, bes$immcul11, method="spearman", use="pairwise")
bescorwelfare <- cor(bes$immwelfare1, bes$immwelfare11, method="spearman", use="pairwise")
bescoravg <- (bescorecon + bescorcul + bescorwelfare)/3
bescoravg <- format(round(bescoravg, digits=3), nsmall = 3)

bescoreconall <- cor(bes$immecon1[bes$all==1], bes$immecon11[bes$all==1], method="spearman", use="pairwise")
bescorculall <- cor(bes$immcul1[bes$all==1], bes$immcul11[bes$all==1], method="spearman", use="pairwise")
bescorwelfareall <- cor(bes$immwelfare1[bes$all==1], bes$immwelfare11[bes$all==1], method="spearman", use="pairwise")
bescoravgall <- (bescoreconall + bescorculall + bescorwelfareall)/3
bescoravgall <- format(round(bescoravgall, digits=3), nsmall = 3)

ncpcor <- cor(ncp$immigration3, ncp$immigration8, method="spearman", use="pairwise")
ncpcor <- format(round(ncpcor, digits=3), nsmall = 3)
ncpcorall <- cor(ncp$immigration3[ncp$all==1], ncp$immigration8[ncp$all==1], method="spearman", use="pairwise")
ncpcorall <- format(round(ncpcorall, digits=3), nsmall = 3)

ncpcor1 <- cor(ncp$imm1_3, ncp$imm1_8, method="spearman", use="pairwise")
ncpcor2 <- cor(ncp$imm2_3, ncp$imm2_8, method="spearman", use="pairwise")
ncpcoravg <- (ncpcor1 + ncpcor2)/2
ncpcoravg <- format(round(ncpcoravg, digits=3), nsmall = 3)

ncpcor1all <- cor(ncp$imm1_3[ncp$all==1], ncp$imm1_8[ncp$all==1], method="spearman", use="pairwise")
ncpcor2all <- cor(ncp$imm2_3[ncp$all==1], ncp$imm2_8[ncp$all==1], method="spearman", use="pairwise")
ncpcoravgall <- (ncpcor1all + ncpcor2all)/2
ncpcoravgall <- format(round(ncpcoravgall, digits=3), nsmall = 3)

inescor <- cor(ines$imm2, ines$imm7, method="spearman", use="pairwise")
inescor <- format(round(inescor, digits=3), nsmall = 3)
inescorall <- cor(ines$imm2[ines$all==1], ines$imm7[ines$all==1], method="spearman", use="pairwise")
inescorall <- format(round(inescorall, digits=3), nsmall = 3)

shpcor <- cor(shp$imm99, shp$imm11, method="spearman", use="pairwise")
shpcor <- format(round(shpcor, digits=3), nsmall = 3)
shpcorall <- cor(shp$imm99[shp$all==1], shp$imm11[shp$all==1], method="spearman", use="pairwise")
shpcorall <-format(round(shpcorall, digits=3), nsmall = 3)

tapscor <- cor(taps$imm8, taps$imm56, method="spearman", use="pairwise")
tapscor <- format(round(tapscor, digits=3), nsmall = 3)
tapscorall <- cor(taps$imm8[taps$all==1], taps$imm56[taps$all==1], method="spearman", use="pairwise")
tapscorall <- format(round(tapscorall, digits=3), nsmall = 3)

glescor <- cor(gles$imm1, gles$imm17, method="spearman", use="pairwise")
glescor <- format(round(glescor, digits=3), nsmall = 3)
glescorall <- cor(gles$imm1[gles$all==1], gles$imm17[gles$all==1], method="spearman", use="pairwise")
glescorall <- format(round(glescorall, digits=3), nsmall = 3)

ccescor <- cor(cces$immigration_10, cces$immigration_14, method="spearman", use="pairwise")
ccescorall <- cor(cces$immigration_10[cces$all==1], cces$immigration_14[cces$all==1], method="spearman", use="pairwise")
ccescor <- format(round(ccescor, digits=3), nsmall = 3)
ccescorall <- format(round(ccescorall, digits=3), nsmall = 3)

ccescor1 <- cor(cces$imm1_10, cces$imm1_14, method="spearman", use="pairwise")
ccescor2 <- cor(cces$imm2_10, cces$imm2_14, method="spearman", use="pairwise")
ccescor3 <- cor(cces$imm3_10, cces$imm3_14, method="spearman", use="pairwise")
ccescoravg <- (ccescor1 + ccescor2 + ccescor3)/3
ccescoravg <- format(round(ccescoravg, digits=3), nsmall = 3)

ccescor1 <- cor(cces$imm1_10[cces$all==1], cces$imm1_14[cces$all==1], method="spearman", use="pairwise")
ccescor2 <- cor(cces$imm2_10[cces$all==1], cces$imm2_14[cces$all==1], method="spearman", use="pairwise")
ccescor3 <- cor(cces$imm3_10[cces$all==1], cces$imm3_14[cces$all==1], method="spearman", use="pairwise")
ccescoravgall <- (ccescor1 + ccescor2 + ccescor3)/3
ccescoravgall <- format(round(ccescoravgall, digits=3), nsmall = 3)

vsgcor <- cor(vsg$immigration_11, vsg$immigration_18, method="spearman", use="pairwise")
vsgcorall <- cor(vsg$immigration_11[vsg$all==1], vsg$immigration_18[vsg$all==1], method="spearman", use="pairwise")
vsgcor <- format(round(vsgcor, digits=3), nsmall = 3)
vsgcorall <- format(round(vsgcorall, digits=3), nsmall = 3)

vsgcor1 <- cor(vsg$imm1_11, vsg$imm1_18, method="spearman", use="pairwise")
vsgcor2 <- cor(vsg$imm2_11, vsg$imm2_18, method="spearman", use="pairwise")
vsgcor3 <- cor(vsg$imm3_11, vsg$imm3_18, method="spearman", use="pairwise")
vsgcoravg <- (vsgcor1 + vsgcor2 + vsgcor3)/3
vsgcoravg <- format(round(vsgcoravg, digits=3), nsmall = 3)

vsgcor1 <- cor(vsg$imm1_11[vsg$all==1], vsg$imm1_18[vsg$all==1], method="spearman", use="pairwise")
vsgcor2 <- cor(vsg$imm2_11[vsg$all==1], vsg$imm2_18[vsg$all==1], method="spearman", use="pairwise")
vsgcor3 <- cor(vsg$imm3_11[vsg$all==1], vsg$imm3_18[vsg$all==1], method="spearman", use="pairwise")
vsgcoravgall <- (vsgcor1 + vsgcor2 + vsgcor3)/3
vsgcoravgall <- format(round(vsgcoravgall, digits=3), nsmall = 3)

z1 <- "("
z2 <- ")"
lisscorall <- paste(z1, lisscorall,z2, sep="")
bescorall <- paste(z1, bescorall,z2, sep="")
ccescorall <- paste(z1, ccescorall,z2, sep="")
vsgcorall <- paste(z1, vsgcorall,z2, sep="")
ncpcorall <- paste(z1, ncpcorall,z2, sep="")
lisscor <- paste(lisscor, lisscorall, sep=" ")
bescor <- paste(bescor, bescorall, sep=" ")
ccescor <- paste(ccescor, ccescorall, sep=" ")
vsgcor <- paste(vsgcor, vsgcorall, sep=" ")
ncpcor <- paste(ncpcor, ncpcorall, sep=" ")

lisscoravgall <- paste(z1, lisscoravgall,z2, sep="")
bescoravgall <- paste(z1, bescoravgall,z2, sep="")
ccescoravgall <- paste(z1, ccescoravgall,z2, sep="")
vsgcoravgall <- paste(z1, vsgcoravgall,z2, sep="")
ncpcoravgall <- paste(z1, ncpcoravgall,z2, sep="")
inescorall <- paste(z1, inescorall,z2, sep="")
shpcorall <- paste(z1, shpcorall,z2, sep="")
tapscorall <- paste(z1, tapscorall,z2, sep="")
glescorall <- paste(z1, glescorall,z2, sep="")

lisscoravg <- paste(lisscoravg, lisscoravgall, sep=" ")
bescoravg <- paste(bescoravg, bescoravgall, sep=" ")
ccescoravg <- paste(ccescoravg, ccescoravgall, sep=" ")
vsgcoravg <- paste(vsgcoravg, vsgcoravgall, sep=" ")
ncpcoravg <- paste(ncpcoravg, ncpcoravgall, sep=" ")
inescor <- paste(inescor, inescorall, sep=" ")
shpcor <- paste(shpcor, shpcorall, sep=" ")
tapscor <- paste(tapscor, tapscorall, sep=" ")
glescor <- paste(glescor, glescorall, sep=" ")

corrflwave <- c(lisscor, bescor, ccescor, vsgcor, ncpcor, "", "", "", "")
avgsingleitem <- c(lisscoravg, bescoravg, ccescoravg, vsgcoravg, ncpcoravg, inescor, shpcor, tapscor, glescor)

numberitems <- c("6","3","3","3","2","1","1","1","1")
timespan <- c("2008-2017", "2014-2017", "2010-2014", "2011-2018", "2014-2017", "2002-2007", "1999-2011", "2012-2016", "2013-2017")
panelnames <- c("LISS", "BES", "CCES", "VSG", "NCP", "INES", "SHP", "TAPS", "GLES")

table <- data.frame(panelnames, timespan, numberitems, corrflwave, avgsingleitem)
table <- xtable(table, type = "latex", latex.environments = "center", caption = "")
print(table, include.rownames=FALSE, include.colnames=FALSE,  sanitize.text.function=identity, only.contents=TRUE,  hline.after = NULL, file=paste(location, "Draft/tables/table2.tex", sep = ""))